﻿using System;

using System.Drawing;

using System.Data;

using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace HSKCards
{
    public partial class Words : Form
    {
        public SqlCeCommand comm;
        public SqlCeConnection conn;
        public SqlCeDataReader result;
        string traditional;
        string simplified;
        string pinyin;
        string english;
        string pos;
        int level;
        bool studylist;
        bool known;
        bool amend;
        short viewcount;

        bool trad;
        

        public string RemoveDuplicateCharacter(string temp)
        {

            StringBuilder a = new StringBuilder(temp);

            for (int i = 0; i < a.Length; i++)
            {
                for (int j = i + 1; j < a.Length; j++)
                {
                    if (a[i] == a[j])
                        a = a.Replace(a[i], (char)46, j, a.Length - j);
                }
            }

            for (int i = 0; i < a.Length; i++)
            {
                int x = Convert.ToInt32(a[i]);
                if (x < 10000 || x > 41000)
                {
                    try
                    {
                        a = a.Remove(i, 1);
                    }
                    catch
                    {
                    }
                    i--;
                }
            }

            return a.ToString();
        }

        public Words(int ID, SqlCeConnection connection, bool t)
        {
            trad = t;
            InitializeComponent();
            conn = connection;
            button1.Font = new Font("PMingLiUCustom", 36, FontStyle.Regular);
            button2.Font = new Font("PMingLiUCustom", 36, FontStyle.Regular);
            button3.Font = new Font("PMingLiUCustom", 36, FontStyle.Regular);
            button4.Font = new Font("PMingLiUCustom", 36, FontStyle.Regular);
            
                
           // get entry from Database
            comm = new System.Data.SqlServerCe.SqlCeCommand();
            comm.Connection = conn;
            comm.CommandText = "Select * from entries where ID = '" + ID.ToString() + "'";
            result = comm.ExecuteReader();

            result.Read();
            //ID = result.GetInt32(0);         
            traditional = result.GetString(1);
            simplified = result.GetString(2);
            pinyin = result.GetString(3);
            english = result.GetString(4);
            pos = result.GetString(5);
            level = result.GetInt32(6);
            studylist = result.GetBoolean(7);
            known = result.GetBoolean(8);
            amend = result.GetBoolean(9);
            viewcount = result.GetInt16(10);

     
            int i = 1;
            string temp;
            if (trad) temp = RemoveDuplicateCharacter(traditional);
            else temp = RemoveDuplicateCharacter(simplified);
            foreach (char c in temp)
            {

                if (i == 1)
                {
                    button1.Text = c.ToString();
                    button1.Visible = true;
                    i++;
                    continue;
                }
                else if (i == 2)
                {
                    button2.Text = c.ToString();
                    button2.Visible = true;
                    i++;
                    continue;
                }
                else if (i == 3)
                {
                    button3.Text = c.ToString();
                    button3.Visible = true;
                    i++;
                    continue;
                }
                else if (i == 4)
                {
                    button4.Text = c.ToString();
                    button4.Visible = true;
                    i++;
                    continue;
                }
                else
                {
                    MessageBox.Show("Word Overflow");
                    continue;
                }
            }

            result.Close();
            comm.Dispose();
            if (temp.Length == 1)
            {
                button1_Click(button1, new EventArgs());
                this.Hide();
                
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Related related = new Related(((Button)sender).Text[0], conn,trad );
            related.ShowDialog();
            related.Close();
            return;
        }
    }
}